package com.stylefeng.guns.modular.system.task;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.core.util.RestUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.CurFengkouMapper;
import com.stylefeng.guns.modular.system.dao.GpcMapper;
import com.stylefeng.guns.modular.system.dao.JjGpcMapper;
import com.stylefeng.guns.modular.system.dao.YdayGpcMapper;
import com.stylefeng.guns.modular.system.model.JjGpc;

@Service
public class JjGpcImportTask {

	private Logger log = LoggerFactory.getLogger(JjGpcImportTask.class);
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	@Autowired
	private GpcMapper gpcMapper;
	
	@Autowired
	private JjGpcMapper jjGpcMapper;
	
	@Autowired
	private YdayGpcMapper ydayGpcMapper;
	
	@Autowired
	private CurFengkouMapper curFengkouMapper;

	public void execute(boolean isHoliday) {
		if(isHoliday){
			List<String> list = gpcMapper.selectCodeListByDate(DateUtil.getAfterDayDate(new Date(), "-1"));
			if(CollectionUtils.isEmpty(list)){
				list = gpcMapper.selectCodeListByDate(DateUtil.getAfterDayDate(new Date(), "-3"));
			}
			List<String> ydayList = ydayGpcMapper.selectCodeListByDate(new Date());
			HttpGet httpGet = HttpClientUtil.getGetRequest();
			try {
				for(String code : list){
					String day = DateUtils.formatDate(new Date(), "yyyyMMdd");
					String url = ImportContants.getJJPathStr(code,day);
						httpGet.setURI(new URL(url).toURI());
						String json = clientUtil.executeAndGetWithRetry(httpGet, 6,10001);
						JSONObject jsonObj = JSON.parseObject(json);
						if (jsonObj.getString("msg").equals("succ")) {
							String desc = jsonObj.getJSONObject("data").getString("desc");
							JSONObject jsonObject = jsonObj.getJSONObject("data").getJSONArray("list").getJSONObject(jsonObj.getJSONObject("data").getJSONArray("list").size()-1);
							int chl = jsonObject.getIntValue("v1");
							if(chl > 0){
								desc += " 剩余 买 " + chl + " 手";
							}else{
								desc += " 剩余 卖 " + chl + " 手";
							}
							
							JjGpc jjGpc = new JjGpc();
							jjGpc.setgScode(code);
							jjGpc.setgDate(new Date());
							jjGpc = jjGpcMapper.selectByDate(jjGpc);
							if(jjGpc == null){
								jjGpc = new JjGpc();
								jjGpc.setgScode(code);
								jjGpc.setgBuy(jsonObject.getDouble("v2"));
								jjGpc.setgDate(new Date());
								jjGpc.setgYdDsc(desc);
								jjGpcMapper.insert(jjGpc);
							}else{
								jjGpc.setgBuy(jsonObject.getDouble("v2"));
								jjGpc.setgYdDsc(desc);
								jjGpcMapper.updateById(jjGpc);
							}
						}
				}
				for(int i=0;i<4;i++){
						for(String code : ydayList){
							String day = DateUtils.formatDate(DateUtil.getAfterDayDate(new Date(), "-" + i), "yyyyMMdd");
							if(!RestUtil.isRest(day)){
								continue;
							}
							String url = ImportContants.getJJPathStr(code,day);
								httpGet.setURI(new URL(url).toURI());
								String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
								JSONObject jsonObj = JSON.parseObject(json);
								if (jsonObj.getString("msg").equals("succ")) {
									String desc = jsonObj.getJSONObject("data").getString("desc");
									JSONObject jsonObject = jsonObj.getJSONObject("data").getJSONArray("list").getJSONObject(jsonObj.getJSONObject("data").getJSONArray("list").size()-1);
									int chl = jsonObject.getIntValue("v1");
									if(chl > 0){
										desc += " 剩余 买 " + chl + " 手";
									}else{
										desc += " 剩余 卖 " + chl + " 手";
									}
									JjGpc jjGpc = new JjGpc();
									jjGpc.setgScode(code);
									jjGpc.setgDate(DateUtil.getAfterDayDate(new Date(), "-" + i));
									jjGpc = jjGpcMapper.selectByDate(jjGpc);
									if(jjGpc == null){
										jjGpc = new JjGpc();
										jjGpc.setgBuy(jsonObject.getDouble("v2"));
										jjGpc.setgScode(code);
										jjGpc.setgDate(DateUtil.getAfterDayDate(new Date(), "-" + i));
										jjGpc.setgYdDsc(desc);
										jjGpcMapper.insert(jjGpc);
									}else{
										jjGpc.setgBuy(jsonObject.getDouble("v2"));
										jjGpc.setgYdDsc(desc);
										jjGpcMapper.updateById(jjGpc);
									}
								}
						}
					}
				} catch (Exception e) {
					log.error("get jj data is error!" + e);
				}finally{
					httpGet.abort();
				}
		}
	}
	
	public static void main(String[] args) {
		HttpGet httpGet = HttpClientUtil.getGetRequest();
		try{
			List<String> list = new ArrayList<String>();
			for(int i=20;i<31;i++){
				String day = "201808" + i;
				String url = ImportContants.getJJPathStr("603032",day);
				httpGet.setURI(new URL(url).toURI());
				String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
				JSONObject jsonObj = JSON.parseObject(json);
				if (jsonObj.getString("msg").equals("succ")) {
					String desc = jsonObj.getJSONObject("data").getString("desc");
					JSONObject jsonObject = jsonObj.getJSONObject("data").getJSONArray("list").getJSONObject(jsonObj.getJSONObject("data").getJSONArray("list").size()-1);
					int chl = jsonObject.getIntValue("v1");
					if(chl > 0){
						desc += " 剩余 买 " + chl + " 手";
					}else{
						desc += " 剩余 卖 " + chl + " 手";
					}
					list.add(desc + "\n");
				}
			}
			System.out.println(list);
		} catch (Exception e) {
			System.out.println();
		}finally{
			httpGet.abort();
		}
	}
}
